package activity
import history.*

class LoginController {

    def index() {
        redirect(action: "login", params: params)        
    }

    def login() {
    }
    
    def loginCheck() {
        def id = params.id
        def pw = params.pw
//println "id:"+id
//println "pw:"+pw
        if(!id | !pw) {
            flash.message = "帳密錯誤，請重新登入"
            redirect(controller:'login', action:'login')
        } else if(id.equals("Administrator") && pw.equals("xinydptl")) {//最終管理權限
//println "IF1"
            flash.message = "系統管理者，歡迎登入，點選上方後台管理功能開始使用"
            session.accountId = "1"
            def account = Account.get("1")
            [account: account]
        } else {
//println "IF2"
            def account = Account.findByLoginAndPw(id, pw)
            if (!account) {
//println "IF3"
                flash.message = "帳密錯誤，請重新登入"
                redirect(controller:'login', action:'login')
            } else {
//println "IF4"
                flash.message = account.name + "，歡迎登入，點選上方後台管理功能開始使用"
                session.accountId = account.id
                [account: account]
            }
//println "IF1"
        }
    }
    def logout() {
        session.accountId = null
        flash.message = "登出完成，請重新登入"
        redirect(controller:'login', action:'login')
    }    
}